package com.xxxx;

import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

import java.util.Arrays;

public class Hello16FlinkOperatorParallelism {

    public static void main(String[] args) throws Exception {
        //获取执行环境
        StreamExecutionEnvironment environment = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(new Configuration());
        //获取数据源
        DataStreamSource<String> source = environment.fromElements("a1 a22 a333 a4444 a55555 a66666", "a1 a22 a333 a4444 a55555 a66666");
        //开始进行转换
        SingleOutputStreamOperator<Tuple2<String, Integer>> dataStream = source.flatMap((String line, Collector<Tuple2<String, Integer>> collector) -> {
            Arrays.stream(line.split(" ")).forEach(word -> collector.collect(Tuple2.of(word, word.length())));
        }).returns(Types.TUPLE(Types.STRING, Types.INT)).setParallelism(2);
        //开始分组操作
        dataStream.keyBy(0).reduce(new ReduceFunction<Tuple2<String, Integer>>() {
            @Override
            public Tuple2<String, Integer> reduce(Tuple2<String, Integer> tuple201, Tuple2<String, Integer> tuple202) throws Exception {
                return Tuple2.of(tuple201.f0, tuple201.f0.length() + tuple202.f0.length());
            }
        }).setParallelism(3).print();
        //开始执行
        environment.execute("Hello16FlinkOperatorParallelism" + System.currentTimeMillis());
    }

}
